﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Prism.Commands;

using VideoPlay.Services;
using VideoPlay.Services.Models;
using VideoPlay.Adapters;

namespace VideoPlay.PhoneClient.ViewModels
{
    public class VideoCategoryViewModel : ViewModel
    {
        private int currentPage = 0;

        public VideoCategoryViewModel(
            INavigationService navigationService,
            IPhoneService phoneService,
            IndicatorViewModel indicator)
            : base(navigationService, phoneService,indicator, new Uri(@"/Views/VideoListView.xaml", UriKind.Relative))
        {
            SearchMoreCommand = new DelegateCommand(this.SearchMore);
        }

        private Category videoCategory;
        public Category VideoCategory
        {
            get
            {
                return this.videoCategory;
            }
            set
            {
                this.videoCategory = value;
                this.RaisePropertyChanged(()=>this.VideoCategory);
            }
        }

        private string itemName = string.Empty;
        public string ItemName
        {
            get { return this.itemName; }
            set
            {
                this.itemName = value;
                this.RaisePropertyChanged(() => this.ItemName);
            }
        }

        private bool isLoadMore = false;
        public bool IsLoadMore
        {
            get { return this.isLoadMore; }
            set
            {
                this.isLoadMore = value;
                this.RaisePropertyChanged(() => this.IsLoadMore);
            }
        }

        private ObservableCollection<VideoViewModel> videos;
        public ObservableCollection<VideoViewModel> Videos
        {
            get 
            {
                if (this.videos == null)
                    this.videos = new ObservableCollection<VideoViewModel>();
                return this.videos; 
            }
            set
            {
                this.videos = value;
                this.RaisePropertyChanged(() => this.Videos);
            }
        }

        public Visibility LoadMoreButtonVisibility
        {
            get
            {
                return Videos.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public DelegateCommand SearchMoreCommand { get; set; }

        private void SearchMore()
        {
            //Indicator.Show();
            IsLoadMore = true;

            currentPage++;
            if (this.VideoCategory.id.Equals("1")) // favorite tab
            {
                int skipCount = (currentPage - 1) * 10;
                int numberOfVideos = Videos.Count;
                int skipNoofRows = skipCount > numberOfVideos ? numberOfVideos : skipCount;
                LoadFavoriteContent(skipNoofRows, 10);
            }
            else
            {
                this.PhoneServiceFacade.RetrieveVideosAsync(Convert.ToInt32(this.VideoCategory.id), currentPage*10, 10, OnReceivedContent);
            }
        }

        public void LoadCategoryContent()
        {
            Indicator.Show();

            currentPage = 0;
            this.Videos = new ObservableCollection<VideoViewModel>();
            if (this.VideoCategory.id.Equals("1")) // favorite tab
            {
                LoadFavoriteContent(0,10);
                currentPage = 1;
            }
            else
            {
                this.PhoneServiceFacade.RetrieveVideosAsync(Convert.ToInt32(this.VideoCategory.id), 0, 10, OnReceivedContent);
            }
        }

        private void LoadFavoriteContent(int page, int numberOfRows)
        {
            List<List> videos = FavoriteFacade.GetPage(page, numberOfRows);
            foreach (var video in videos)
            {
                VideoViewModel vm = new VideoViewModel(NavigationService, PhoneServiceFacade,Indicator);
                vm.ImageUrl = video.image;
                vm.VideoName = video.title;
                vm.Duration = video.lcDuration;
                vm.VideoUrl = video.url;
                vm.ID = video.lcLocalId;
                vm.YoutubeId = string.IsNullOrEmpty(video.lcYoutubeId) ? null : video.lcYoutubeId;
                vm.Like = true;
                

                this.Videos.Add(vm);
            }
            this.RaisePropertyChanged(() => this.LoadMoreButtonVisibility);
            Indicator.Hide();
        }

        private void OnReceivedContent(object param)
        {
            List<Videos> videos = param as List<Videos>;
            foreach (var video in videos)
            {
                VideoViewModel vm = new VideoViewModel(NavigationService,PhoneServiceFacade,Indicator);
                vm.ImageUrl = string.Format("http://blebleble.byethost22.com/mtes/{0}", video.List.image);
                vm.VideoName = video.List.title;
                vm.Duration = video.List.duration.ToString();
                vm.VideoUrl = video.List.url;
                vm.ID = video.List.id;
                vm.YoutubeId = string.IsNullOrEmpty(video.List.youtubeId) ? null : video.List.youtubeId;
                vm.Like = IsFavoriteVideo(video.List.id, vm.YoutubeId);

                this.Videos.Add(vm);
            }
            this.RaisePropertyChanged(() => this.LoadMoreButtonVisibility);
            this.Indicator.Hide();
            IsLoadMore = false;
        }

        private bool IsFavoriteVideo(int id,string youtubeId)
        {
            List video = FavoriteFacade.GetVideoById(id,youtubeId);
            return video == null ? false : true;
        }

        public override void OnPageResumeFromTombstoning()
        {

        }
    }
}
